package utils.trtb.stringutils {
	
	public class StringUtil {
		
	
		//천단위 콤마. 소수점 관리  
		static public function comma($number:Number, $pointUnderMax:uint=0):String {
			if ($number>Number.MAX_VALUE || $number<-Number.MAX_VALUE) throw new Error('범위를 벗어났습니다.');
			
			var a:Array = [];
			
			var u:String  = ($pointUnderMax && $number%1) ? '.' + $number.toString().split('.')[1].slice(0, $pointUnderMax) : '';
			var s:String  = $number>=0 ?  ($number-$number%1).toString() : ($number%1 - $number).toString();
			var sn:String = $number>=0 ? '' : (Number(s)<1 && u=='') ? '':'-';
			
			for (var i:int=s.length; i>0; i-=3) {
				a.unshift( (i-3) < 0 ? s.slice(0, i) : s.slice(i-3, i) );
			}
			return sn + a.join(',') + u;
		}
		
		
		private function leftTrim( _str:String ):String {
			var str:String = _str;
			var len:int = str.length;
			var startIdx:int;
			
			for ( var i:int = 0; i < len; ++i ) {
				if ( str.charAt(i) != " " ) {
					startIdx = i;
					break;
				}
			}
			
			return str.substring( startIdx, len );
		}
		
		private function rightTrim( _str:String ):String {
			var str:String = _str;
			var len:int = str.length;
			var startIdx:int;
			
			for ( var i:int = len-1; i >= 0; --i ) {
				if ( str.charAt(i) != " " ) {
					startIdx = i;
					break;
				}
			}
			
			return str.substring( 0, startIdx );
		}
		
		//공백 트림.
		public static function trim( _str:String ):String {
			return leftTrim( rightTrim( _str ) );
		}
	}
}